﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">关键字</label>
                <div class="layui-input-inline" style="width:400px;">
                    <input type="text"  name="channelName" placeholder="可输入栏目名称、索引、栏目组" class="layui-input">
                </div>
                 
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="channel-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="channel-table" lay-filter="channel-table"></table>
    </div>
</div>


<script type="text/html" id="channel-toolbar">

    @{ await Html.RenderPartialAsync("_ToolbarAuthTpl", new SystemFunctionModel
        {
            MenuKey = SysMenuConstant.MENU_CMS_NEWS_LMGL,
            FunDictionary = new Dictionary<string, string> { { "BatchExpand", "展开" }, { "BatchFold", "折叠" },{ "Add", "新增" }, { "Taxis", "排序" }, { "More","更多" } }
        });
    }
</script>

@section scripts{
    <script>
   
    layui.use(['table', 'form', 'lscom', 'treetable','dropdown'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;
        let lscom = layui.lscom;
        let dropdown = layui.dropdown;
        let MODULE_PATH = "/Cms/Channel/";

        var istoolbar = '@ViewBag.AuthAll' =='1' ? true : false;

        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pId',
            skin: 'line',
            treeDefaultClose: true,
            toolbar: '#channel-toolbar',
            elem: '#channel-table',
            url: MODULE_PATH+'GetAll',
            page: false,
            cols: [
                [
                    { type: 'checkbox', width: 60 },
                    { field: "name", title: "栏目名称", width: 180 },
                    { field: "indexName", title: "栏目索引", width: 180 },
                    { field: "groupNames", title: "栏目组"},
                    { title: "操作", width: 160, align: "center", fixed: "right", templet: function(d){
                        if (d.id == 1) {
                            return '@ViewBag.AuthUpdate'=='1'? '<button class="pear-btn pear-btn-xs" lay-event="Update">编辑</button>':'';
                        }
                        else{
                            return ('@ViewBag.AuthUpdate' == '1' ? '<button class="pear-btn pear-btn-xs" lay-event="Update">编辑</button>' : '') + ('@ViewBag.AuthDelete' == '1' ? '&nbsp;<button class="pear-btn pear-btn-danger pear-btn-xs" lay-event="Delete">删除</button>':'');
                        }
                    },hide:istoolbar }
                ]
            ],
            done: function(res, curr, count) {
                 dropdown.render({
                        elem: '.btnmore'
                        , data: [{
                            title: '分组'
                            ,id: 100
                        },{
                            title: '复制'
                            ,id: 101
                        }]
                        , click: function(obj) {
                            if(obj.id==100){
                                    let checkData = table.checkStatus("channel-table").data;
                                    if (checkData.length === 0) {
                                        parent.layer.msg("未选中数据", {
                                            icon: 3,
                                            time: 1000
                                        });
                                        return false;
                                    }
                                    var ids = checkData.map(function (d) { return d.id; });
                                   window.group(ids.join(','));
                            }
                            if(obj.id==101){
                                
                                   window.translate();
                            }
                        }
                    }); 
            }
        });

        table.on('tool(channel-table)', function (obj) {
            if (obj.event === 'Delete') {
                window.remove(obj.data['id']);
            } else if (obj.event === 'Update') {
                window.edit(obj.data['id']);
            } else if (obj.event === 'Add') {
                window.add(obj.data['id']);
            }
        })
     
           
        table.on('toolbar(channel-table)', function (obj) {
            if (obj.event === 'Add') {
                window.add();
            } 
            else if (obj.event === 'Delete') {
               
                let checkData = table.checkStatus(obj.config.id).data;
                if (checkData.length === 0) {
                    parent.layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = checkData.map(function (d) { return d.id; });
                window.remove(ids.join(','));
            } else if (obj.event === 'BatchExpand') {
                treetable.expandAll("#channel-table");
            } else if (obj.event === 'BatchFold') {
                treetable.foldAll("#channel-table");
            } else if (obj.event === 'Refresh') {
                treetable.reload("#channel-table");
            }else if(obj.event === 'Taxis'){
                
                    let checkData = table.checkStatus(obj.config.id).data;
                    if (checkData.length === 0) {
                        parent.layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    var ids = checkData.map(function (d) { return d.id; });
                    window.taxis(ids.join(','));
            }
        });

        form.on('submit(channel-query)', function (data) {
            var keyword = data.field.channelName;
            treetable.search('#channel-table', keyword);
            return false;
        });
       
        window.add = function (pid) {
                var conditaion = '';
                if (pid) {
                    conditaion = '?pid=' + pid;
                }
            parent.layer.open({
                type: 2,
                title: '新增栏目',
                shade: 0.1,
                area: ['750px', '700px'],
                content: MODULE_PATH + 'Create' + conditaion,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    parent.window['layui-layer-iframe' + index].submitForm();
                }
            });
        }

        window.edit = function (id) {
            parent.layer.open({
                type: 2,
                title: '修改栏目',
                shade: 0.1,
                area: ['80%', '70%'],
                content: MODULE_PATH + 'Edit?id=' + id,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    parent.window['layui-layer-iframe' + index].submitForm();
                }
            });
        }

        window.remove = function (data) {
            parent.layer.confirm('确定删除吗?', {
					icon: 3,
					title: '提示'
                }, function (index) {
                    parent.layer.close(index);
					let waitIndex = parent.layer.load(2);
                    lscom.ajax({
                        url: MODULE_PATH + 'Del',
                        data: JSON.stringify({ "ids": data }),
                        lsHandleError: false
                    }).done(function (data) {
                        if (data.code == 0) {
                            lscom.notify.success(data.message);
                            treetable.reload("#channel-table");
                        }
                    }).fail(function (message) {
                        lscom.notify.error(message);
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
        }

         window.group = function (id) {
            parent.layer.open({
                type: 2,
                title: '设置栏目组',
                shade: 0.1,
                area: ['50%', '50%'],
                content: MODULE_PATH + 'Group?id=' + id,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    parent.window['layui-layer-iframe' + index].submitForm();
                }
            });
        }

        window.taxis = function (id) {
            parent.layer.open({
                type: 2,
                title: '栏目排序',
                shade: 0.1,
                area: ['50%', '50%'],
                content: MODULE_PATH + 'Taxis?id=' + id,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    parent.window['layui-layer-iframe' + index].submitForm();
                }
            });
        }
         window.translate = function (id) {
           var  muilttab = localStorage.getItem("muilt-tab");
                                if (muilttab=="true") {
                                parent.layui.admin.closeTab(100161);
                        }
           parent.layui.admin.jump(100161,"栏目复制",MODULE_PATH + 'Translate');
        }

        window.saveCallback = function (data) {
            parent.layer.close(data.index);
            lscom.notify.success(data.message);
            treetable.reload("#channel-table");
        }
    })
       
    </script>
}

